#ifndef PARAMETER_LOADER_C_
#define PARAMETER_LOADER_C_

#include "parameter_loader.h"


int load_parameter(char * file_conf){
		 
	FILE *fp;
	
	char file_row[255];
	char temp_row[255];

	char *pointer_temp_row;
	char *pointer_temp_row_fin;
	
	char param[100];
	char value[100];
	
	int s=0;
		
	printf("[Loading configuration file \"%s\"... ]\n", file_conf);
	
	fp = fopen (file_conf, "r");
	
	if(fp == NULL) {
		printf_error();
		return 1;
	}
			

	while(fgets(file_row, 255, fp)) {
		
		strcpy(temp_row, file_row);
		pointer_temp_row = strstr(temp_row, "=");
		strncpy(pointer_temp_row, "\0", 1);
		strcpy(param, temp_row);
		pointer_temp_row++;
		strcpy(value, pointer_temp_row);
		pointer_temp_row = strstr(value,"\"");
		
		if(pointer_temp_row == NULL){
			printf("File configuration error: missing \" at the beginning of parameter value\n");
			return 1;
		}
		
		else {
			pointer_temp_row++;
			strcpy(value,pointer_temp_row--);
			pointer_temp_row_fin = strstr(value, "\"");
			
			if(pointer_temp_row_fin == NULL){
				printf("File configuration error: missing \" at the end of parameter value\n");
				return 1;
			}
			
			else {
				strncpy(pointer_temp_row_fin, "\0", 1);
				strcpy(value,pointer_temp_row);
				
				if((strcmp(param, "port")) == 0)
					config.port_listen = atoi(value);
					
				else if((strcmp(param, "bootstrap_file")) == 0)
					strcpy(config.bootstrap_file, value);
						
				else if((strcmp(param,"user_name")) == 0)
					strcpy(config.user_name, value);
				
				else if((strcmp(param,"user_id")) == 0)
					strcpy(config.user_id, value);
			}
		}
		
		pointer_temp_row=NULL;
		
		strcpy(param,"\0");strcpy(value,"\0");
	} 

	fclose(fp);
	
	printf("[Config Loaded:]\n");
	
	printf("---->[user_name: %s]\n", config.user_name);
	printf("---->[user_id: %s]\n", config.user_id);
	printf("---->[listen port: %d]\n", config.port_listen);
	printf("---->[bootstrap_file: %s]\n", config.bootstrap_file);

	return(0);
}


#endif
